package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.javabean.Orders;
import com.yh.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {
    @Autowired
    OrdersService ordersService;
    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page",required = false, defaultValue = "1") int page,
                          @RequestParam(value = "limit", required = false, defaultValue = "5") int limit,
                          @RequestParam(value = "key",required = false,defaultValue = "")String key,
                          @RequestParam(value = "value",required = false,defaultValue = "")String value,
                          HttpServletRequest request) {
        PageHelper.startPage(page,limit);
        List<Orders> orders= null;
        if("productName".equals(key)){
            orders=ordersService.findAll("",value);
        }else if("orderNum".equals(key)){
            orders=ordersService.findAll("",key);
        }else{
            orders=ordersService.findAll();
        }
        PageInfo<Orders> pageInfo=new PageInfo<>(orders);
        request.setAttribute("select",key);
        request.setAttribute("selectValue",value);
        request.setAttribute("pageInfo",pageInfo);
        return "/orders/orders-list";
    }
    @RequestMapping("/findById")
    public String findById(HttpServletRequest request,int id){
        Orders orders = ordersService.findById(id);
        request.setAttribute("orders",orders);
        return "/orders/orders-show";
    }
    @RequestMapping("/josn")
    @ResponseBody
    public PageInfo<Orders> json(@RequestParam(value = "page",required = false,defaultValue = "1") int page,
                                 @RequestParam(value = "limit",required = false,defaultValue = "3")int limit,
                                 @RequestParam(value = "key",required = false,defaultValue = "")String key,
                                 @RequestParam(value = "value",required = false,defaultValue = "")String value
    ){
        PageHelper.startPage(page, limit);
        List<Orders> orders =null;
        if("productName".equals(key)){
            orders=ordersService.findAll("",value);
        }else if("orderNum".equals(key)){
            orders=ordersService.findAll(value,"");
        }else{
            orders=ordersService.findAll("","");
        }
        PageInfo<Orders> pageInfo=new PageInfo<>(orders);
        return pageInfo;
    }
    @RequestMapping("/show")
    @ResponseBody
    public Orders json(int id){
        Orders orders = ordersService.findById(id);
        return orders;
    }
}
